package mythreads;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

class SynchronizedOutput {
	private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); 
	private final Lock read  = readWriteLock.readLock();
	  
	  private final Lock write = readWriteLock.writeLock();

	// if the 'synchronized' keyword is removed, the message
	// is displayed in random fashion
	public  synchronized void displayList(String name, String list[]) {
		read.lock();
		try{
			for (int i = 0; i < list.length; i++) {
				System.out.println(name + list[i]);
			}	
			
		}finally{
			read.unlock();
		}
		
	}
}

class ThreadExtnd extends Thread{
	
	static String[] msg = { "Beginner", "java", "tutorial,", ".,", "com",
		"is", "the", "best" };
	
	@Override
	public synchronized void run(){
		System.out.println("Hello ThreadExtnd");
		new SynchronizedOutput().displayList(Thread.currentThread().getName(), msg);
	}
}

class RunnableImpl implements Runnable{

	public void run() {
		System.out.println("Hello RunnableImpl");
	}
}

public class FirstThread {
	
	public static void main(String[] args) {
       Thread t = new ThreadExtnd();
       t.start();
      /* new Thread(){
    	  public void run(){
    		  System.out.println("hello Anon thread");
    	  }
       }.start();*/
	}

}
